/**
 * @author Greg Feigenson
 * @version RC1
 * @date 05/10/2004
 *
 * The UI component of the conf tool. This code comes from
 * IRCD Phoenix (http://ircd-phoenix.sourceforge.net) which is
 * released under the BSD license.
 *
 * Please read BSD.LICENSE, found in this directory.
 */

package conf;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

class ConfTool extends JFrame implements ActionListener, ListSelectionListener {
    //
    // Constructor
    //
    public ConfTool() {
        // GUI Setup
        setTitle("Heart of Gold Conf Utility");
        setSize(310, 370);
        setBackground(Color.gray);

        // Top Panel
        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        // Setup the list
        //refresh(listElements);

        list = new JList(listElements);
        list.addListSelectionListener(this);

        scrollPane = new JScrollPane();
        scrollPane.getViewport().add(list);
        topPanel.add(scrollPane, BorderLayout.CENTER);

        // Other housekeeping
        SetupPanels();
        setupMenu();
    }

    // Handler for list selection changes
    public void valueChanged(final ListSelectionEvent event) {
        // make sure list cursor is settled
        if (event.getSource() == list && !event.getValueIsAdjusting()) {
            String stringValue = (String) list.getSelectedValue();

            if (stringValue != null) {
                addText.setText(stringValue);
            }
        }
    }

    // Action Handler for buttons being pressed
    public void actionPerformed(final ActionEvent event) {
        String command = event.getActionCommand();
        // TODO: add input checking
        if (command.equals("Add")) {
            String stringValue = addText.getText();
            if (stringValue != null && !stringValue.equals("")) {
                listElements.addElement(stringValue);
                list.setListData(listElements);
                scrollPane.revalidate();
                scrollPane.repaint();
            }
        }
        if (command.equals("Delete")) {
            // Get the current selection
            int selection = list.getSelectedIndex();
            if (selection >= 0) {
                // Add this item to the list and refresh
                listElements.removeElementAt(selection);
                list.setListData(listElements);
                scrollPane.revalidate();
                scrollPane.repaint();

                // Select the next item
                if (selection >= listElements.size()) {
                    selection = listElements.size() - 1;
                }
                list.setSelectedIndex(selection);
            }
        }
        if (command.equals("Exit")) {
            System.exit(0);
        }
        if (command.equals("Save")) {
            parser.writeConf(listElements);
        }
        if (command.equals("Refresh")) {
            refresh(listElements);
            list.setListData(listElements);
        }
        if (command.equals("About")) {
            JOptionPane
                    .showMessageDialog(
                            this,
                            "Heart of Gold - Configuration Utility\nWritten by "
                                    + "Greg Feigenson\nReleased under BSD protection"
                                    + "\n\nCheck out http://heartofgold.sourceforge.net",
                            "About Heart of Gold Conf Utility",
                            JOptionPane.INFORMATION_MESSAGE);
        }
        if (command.equals("Help!")) {
            JOptionPane.showMessageDialog(this,
                    "The configuration editor is a graphical"
                            + " tool to write your configuration file.\n"
                            + "Click an item and hit delete, or type into"
                            + " the textbox and click add.\n"
                            + "When done, click 'save' and exit the program.\n"
                            + "\nPlease run BEFORE you run Heart of Gold",
                    "Help!", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    //
    // Private Methods
    //
    private void refresh(final Vector v) {
        v.clear(); // flush the vector
        parser.setFile("hog.conf");
        Hashtable temp = parser.parse();
        Enumeration hashKeys = temp.keys();
        while (hashKeys.hasMoreElements()) {
            String key = (String) hashKeys.nextElement();
            String composedValue = key + ":" + temp.get(key);
            v.add(composedValue);
        }
    }

    private void SetupPanels() {
        // Parent Panel
        JPanel dataPanel = new JPanel();
        dataPanel.setLayout(new BorderLayout());
        topPanel.add(dataPanel, BorderLayout.SOUTH);

        // Logo
        topPanel.add(logo, BorderLayout.NORTH);

        // JButtons
        addButton = new JButton("Add");
        dataPanel.add(addButton, BorderLayout.WEST);
        addButton.addActionListener(this);
        delButton = new JButton("Delete");
        dataPanel.add(delButton, BorderLayout.EAST);
        delButton.addActionListener(this);
        saveButton = new JButton("Save");
        saveButton.addActionListener(this);
        refreshButton = new JButton("Refresh");
        refreshButton.addActionListener(this);
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);

        // Add Text
        addText = new JTextField();
        dataPanel.add(addText, BorderLayout.CENTER);

        // Save/Refresh Button
        dataPanel.add(bottomPanel, BorderLayout.SOUTH);
        bottomPanel.add(saveButton);
        bottomPanel.add(refreshButton);
        bottomPanel.add(exitButton);
    }

    private void setupMenu() {
        // Menu Bar
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // File Menu
        JMenu menuFile = new JMenu("File");
        JMenuItem menuExit = new JMenuItem("Exit", KeyEvent.VK_X);
        menuFile.setMnemonic(KeyEvent.VK_F);
        menuExit.addActionListener(this);
        menuBar.add(menuFile);
        menuFile.add(menuExit);

        // Help Menu
        JMenu menuHelp = new JMenu("Help");
        JMenuItem menuAbout = new JMenuItem("About", KeyEvent.VK_A);
        JMenuItem menuHelpMe = new JMenuItem("Help!", KeyEvent.VK_E);
        menuHelp.setMnemonic(KeyEvent.VK_H);
        menuAbout.addActionListener(this);
        menuHelpMe.addActionListener(this);
        menuBar.add(menuHelp);
        menuHelp.add(menuAbout);
        menuHelp.add(menuHelpMe);
    }

    //
    // Private data-members
    //
    private JMenuBar menuBar;
    private JPanel topPanel;
    private JPanel bottomPanel = new JPanel(new FlowLayout());
    private JList list;
    private Vector listElements = new Vector();
    private JButton addButton;
    private JButton delButton;
    private JButton saveButton;
    private JButton refreshButton;
    private JButton exitButton;
    private JTextField addText;
    private JScrollPane scrollPane;
    private ConfParser parser = ConfParser.getParser();
    private JLabel logo = new JLabel(new ImageIcon("HOG_logo.png"));
}
